home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-31 | 1.5 KB | 80 lines | [TEXT/CWIE] |
- #include <iostream.h>
- #include <string.h>
-
-
- //--------------------------------------- String
-
- class String
- {
- private:
- char *s;
- short stringLength;
-
- public:
- String( char *theString );
- ~String();
- void DisplayAddress();
- String &operator=( const String &fromString );
- };
-
- String::String( char *theString )
- {
- stringLength = strlen( theString );
- s = new char[ stringLength + 1 ];
-
- strcpy( s, theString );
- }
-
- String::~String()
- {
- delete [] s;
- }
-
- void String::DisplayAddress()
- {
- // I added an extra line to the DisplayAddress function
- // because both sets of address in the program were
- // turning out to be the same. I now print out the string
- // along with the string address. Now when you run the program,
- // you can see that the first time you print captain and doctor,
- // they contain different strings, but the second time, they
- // contain the same string, even though their addresses
- // didn't change.
- // Sorry for any confusion -- Dave Mark 10/31//95
- cout << "String address: " << (unsigned long)s << "\n";
- cout << " content: " << s << "\n\n";
- }
-
- String &String::operator=( const String &fromString )
- {
- delete [] s;
-
- stringLength = fromString.stringLength;
-
- s = new char[ stringLength + 1 ];
-
- strcpy( s, fromString.s );
-
- return( *this );
- }
-
-
- //--------------------------------------- main()
-
- int main()
- {
- String captain( "Picard" );
- String doctor( "Crusher" );
-
- captain.DisplayAddress();
- doctor.DisplayAddress();
-
- cout << "-----\n";
-
- doctor = captain;
-
- captain.DisplayAddress();
- doctor.DisplayAddress();
-
- return 0;
- }